/*
 * Copyright (c)  2020,  DataPipeline
 * All rights reserved.
 *
 * Id:OrmCoreException.java   2020-07-15 00:28 wanglijun
 */
package cn.datacare.orm.exception;

import java.text.MessageFormat;

import static cn.datacare.orm.util.Separator.COLON;

/**
 * <p>
 * Title: DateCareOrmException (核心异常处理类)
 * </p>
 * <p>
 * Description: DateCareOrmException (核心异常处理类)
 * </p>
 * <p>
 * Copyright: Copyright (c) 2020
 * </p>
 * <p>
 * Company/Department: DataPipeline
 * </p>
 *
 * @author wanglijun
 * @version 1.0
 * @since 2020-07-15 00:28
 */
public class OrmCoreException extends RuntimeException {
    /***
     * 错误代码
     */
    protected String code;


    /**
     * 错误消息
     *
     * @param message 错误消息
     */
    public OrmCoreException(String message) {
        super(message);
    }

    /***
     * @param cause  错误异常
     */
    public OrmCoreException(Throwable cause) {
        super(cause);
    }

    /***
     * @param cause  错误异常
     */
    public OrmCoreException(String message, Throwable cause) {
        super(message, cause);
    }

    /***
     * 默认异常处理
     * @param code 错误代码
     * @param message 错误信息
     */
    public OrmCoreException(String code, String message) {
        super(code + COLON + message);
        this.code = code;
    }

    /***
     * 默认异常处理错误消息
     * @param code 错误代码
     * @param message 错误信息
     * @param  params 参数
     */
    public OrmCoreException(String code, String message, Object... params) {
        super(code + COLON + message);
        this.code = code;
    }

    /**
     * 错误代码，错误消息、cause
     *
     * @param code    错误代码
     * @param message 错误消息
     * @param cause   param cause the cause.  (A {@code null} value is permitted,
     */
    public OrmCoreException(String code, String message, Throwable cause) {
        super(code + COLON + message, cause);
        this.code = code;
    }

    /**
     * 错误代码，错误消息、cause, params
     *
     * @param code    错误代码
     * @param message 错误消息
     * @param cause   param cause the cause.  (A {@code null} value is permitted,
     * @param params  参数
     */
    public OrmCoreException(String code, String message, Throwable cause, Object... params) {
        this(code, MessageFormat.format(message, params), cause);
    }

    /***
     * 错误代码
     * @param errorCode 错误代码（code,Message)
     */
    public OrmCoreException(ErrorCode errorCode) {
        this(errorCode.getCode(), errorCode.getMessage());
    }

    /***
     * 错误代码&错误参数
     * @param errorCode  错误代码（code,message)
     * @param params 参数
     */
    public OrmCoreException(ErrorCode errorCode, Object... params) {
        this(errorCode.getCode(), errorCode.getMessage(params));
    }

    /***
     * 错误代码&错误参数
     * @param errorCode  错误代码（code,message)
     * @param cause Throwable
     * @param params 参数
     */
    public OrmCoreException(ErrorCode errorCode, Throwable cause, Object... params) {
        this(errorCode.getCode(), errorCode.getMessage(params), cause);
    }

    /***
     * 错误代码&错误参数
     * @param errorCode  错误代码（code,message)
     * @param cause Throwable
     */
    public OrmCoreException(ErrorCode errorCode, Throwable cause) {
        this(errorCode.getCode(), errorCode.getMessage(), cause);
    }

    public String getCode() {
        return code;
    }
}
